const Base = require('./base.js');
module.exports = class extends Base {
  __before() {
    super.__before();
    this.commentsService = think.rpc.information.comments;
    this.userService = think.rpc.user.user;
  }
  // 评论
  async createAction() {
    const informationId = this.intVal('informationId');
    const content = this.strVal('content');
    const toUserId = this.intVal('toUserId') || 0; // 可以为空
    think.assertUtil.isEmpty(content, '回复内容不能为空');
    think.assertUtil.isEmpty(informationId, 'informationId不能为空');
    const newId = await this.commentsService.create({
      userId: this.getUserId(),
      informationId,
      toUserId,
      content
    });
    return this.success(newId);
  }
  // 删除用户评论
  async removeAction() {
    const id = this.intVal('id');
    const lastCommentsId = this.intVal('lastCommentsId', 0);
    const informationId = this.intVal('informationId', 0);
    let lastComments = null;
    const userId = this.getUserId();
    if (lastCommentsId) {
      lastComments = await this.commentsService.queryNextCommentOfOneId(lastCommentsId, informationId);
      think.logger.info(lastComments);
      if (!think.isEmpty(lastComments)) {
        const userIds = [];
        userIds.push(lastComments.user_id);
        userIds.push(lastComments.to_user_id);
        const userRows = await this.userService.queryByUserIds(userIds, 'id,nickname,avatar');
        const userRow = think.oneOrDefault(userRows, el => el.id === lastComments.user_id, {});
        lastComments.nickname = userRow.nickname || '';
        lastComments.avatar = userRow.avatar || '';
        const toUserRow = think.oneOrDefault(userRows, el => el.id === lastComments.to_user_id, {});
        lastComments.toNickname = toUserRow.nickname || '';
        lastComments.isMine = userId === lastComments.user_id;
      } else {
        lastComments = null;
      }
    }
    await this.commentsService.removeUserCommentById(id, userId);
    return this.success(lastComments);
  }
  // 查找用户发布的评论
  async commentsOfUserPublishAction() {
    const userId = this.getUserId();
    const pageInfo = this.pageInfo();
    const rows = await this.commentsService.queryDataListByUserId(userId, pageInfo);
    const userIdMap = {};
    rows.forEach((row, idx) => {
      if (row.to_user_id > 0) {
        if (!userIdMap[row.to_user_id]) {
          userIdMap[row.to_user_id] = [];
        }
        userIdMap[row.to_user_id].push(idx);
      }
      row.create_time = think.timeUtil.beforeTimeFormat(row.create_time);
    });
    const userIds = Object.keys(userIdMap);
    if (userIds && userIds.length) {
      const userRows = await this.userService.queryByUserIds(userIds, 'id,nickname');
      userIdMap.keys().forEach(key => {
        key = parseInt(key);
        const value = userIdMap[key];
        value.forEach(idx => {
          think.extend(rows[idx], think.oneOrDefault(userRows, el => el.id === key));
        });
      });
    }
    return this.success(rows);
  }
  // 查询某信息下用户发布的所有评论
  async queryCommentsOfInformationAction() {
    const pageInfo = this.pageInfo();
    const informationId = this.intVal('informationId') || 0;
    const rows = await this.commentsService.queryCommentsOfInformation(informationId, pageInfo);
    const userIdMap = {};
    const userId = this.getUserId();
    rows.forEach(row => {
      if (row.user_id && !userIdMap[row.user_id]) {
        userIdMap[row.user_id] = 1;
      }
      if (row.to_user_id && !userIdMap[row.to_user_id]) {
        userIdMap[row.to_user_id] = 1;
      }
      row.create_time = think.timeUtil.beforeTimeFormat(row.create_time);
      row.isMine = userId === row.user_id;
    });
    const userIds = Object.keys(userIdMap);
    if (userIds.length) {
      const userRows = await this.userService.queryByUserIds(userIds, 'id,nickname,avatar');
      rows.forEach(row => {
        const userRow = think.oneOrDefault(userRows, el => el.id === row.user_id, {});
        row.nickname = userRow.nickname || '';
        row.avatar = userRow.avatar || '';
        const toUserRow = think.oneOrDefault(userRows, el => el.id === row.to_user_id, {});
        row.toNickname = toUserRow.nickname || '';
      });
    }
    return this.success({data: rows, hasNoMore: rows.length < pageInfo.pageSize});
  }
};
